﻿using Microsoft.Deployment.WindowsInstaller;
using Microsoft.Deployment.WindowsInstaller.Package;
using SnShared.Models;
using SwidHelperLib;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;

namespace SnClient
{
    public class InstalledPackagesHelper
    {
        private static Boolean IsExecutable(String fileName)
        {
            if (fileName.Contains(".exe") || fileName.Contains(".dll"))
                return true;

            return false;
        }

        public static void GetInstalledFiles(CDMRestApi restClient)
        {

            //
            // Enumerate packages via managed installer helper
            //

            int count = 0;
            int fileCount = 0;

            List<SoftwarePackage> packages = new List<SoftwarePackage>();


            foreach ( ProductInstallation productInstallation in ProductInstallation.GetProducts(
                null, null, Microsoft.Deployment.WindowsInstaller.UserContexts.Machine ) )
            {
                if ( String.IsNullOrEmpty( productInstallation.LocalPackage ) )
                {
                    continue;
                }

                count++;
                
                SoftwarePackage installedPackage = new SoftwarePackage();
                installedPackage.Files = new List<InstalledFile>();
                installedPackage.Name = productInstallation.AdvertisedProductName;                    
                installedPackage.UniqueId = productInstallation.ProductCode;
                installedPackage.SwidTag = SwidHelper.GetSwidTagStringForProduct(
                    null,
                    productInstallation.ProductCode,
                    productInstallation.AdvertisedProductName,
                    productInstallation.ProductVersion,
                    productInstallation.Publisher);
                                
                using ( InstallPackage installPackage = new InstallPackage(
                    productInstallation.LocalPackage, DatabaseOpenMode.ReadOnly ) )
                {
                    try
                    {
                        foreach ( var filePath in installPackage.Files )
                        {                            
                            String fName = Path.GetFileName(filePath.Value.TargetPath);
                            if (!IsExecutable(fName))
                                continue;
                            fileCount++;

                            installedPackage.Files.Add(new InstalledFile() { Name = fName });                
                        }
                    }
                    catch ( ArgumentException )
                    {
                        // Seems to be a bug in file enumeration for some 
                        // packages. Move to the next one.
                    }
                }

                packages.Add(installedPackage);

                //AddPackage addPackageRequest = new AddPackage() { ComputerName = Environment.MachineName, Package = installedPackage };
                //restClient.PostPackage(addPackageRequest);
            }

            AddPackage addPackageRequest = new AddPackage() { ComputerName = Environment.MachineName, Package = packages };
            restClient.PostPackage(addPackageRequest);



            Console.WriteLine("Packages {0}  Files {1}", count, fileCount);

        }
    }
}
